﻿<!doctype html>
<html><!-- InstanceBegin template="/Templates/template1.dwt" codeOutsideHTMLIsLocked="false" -->
<head>
<meta charset="utf-8">
<!-- InstanceBeginEditable name="doctitle" -->
<title>积分规则管理</title>
<!-- InstanceEndEditable -->
<script type="text/javascript" src="../js/common.js"></script>
<script type="text/javascript" src="../js/qmsk-1.2.js"></script>
<link href="../css/Pager.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="../js/jquery.pager.js"></script>
<script type="text/javascript" src="../js/gbRst.js"></script>
<script type="text/javascript" src="../js/utils.js"></script>
<link href="../css/style.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="../js/checkLogin.js"></script>
<script type="text/javascript" src="../js/pagination.js"></script>
<script type="text/javascript" src="../js/doubleDate2.0.js"></script> 
<script type="text/javascript" src="../js/find.js"></script> 
<script type="text/javascript" src="../js/commonSelecter.js"></script>
<!-- InstanceBeginEditable name="head" -->
<!-- InstanceEndEditable -->
<script type="text/javascript" >
var _pagination = null;
var _schedTypeId = null;
$(function(){
	refreshInput($(".serWrapper"));
	$('.doubledate').kuiDate({
		className:'doubledate',
		isDisabled: "0"  // isDisabled为可选参数，“0”表示今日之前不可选，“1”标志今日之前可选
	});
});
function bclicks(obj){
	var $first=obj.parent("td").parent("tr").children("td:first");
	var id=$first.html();
	var name=$first.next().html();
	//$("#shopName").children("option").blur();
	getParamToshopS(id,name)
}
var pageIndex = 1;
var pagecount = 0;
var count = 0;
var shopId="shopId";
var shopPageCount=0;
var shopCount=0;
var schedulePageIndex = 1;
var schedulePageCount =0;
$(function(){

	$("#shopIsChoosed").click(function(){
		if($("#shopIsChoosed").is(":checked")){
			$("#shopName").attr("disabled",false);
		}else{
			$("#shopName").attr("disabled",true);
		}
	})
	
	addMargin($("#window_change"));
	
	$("#chooseJifen").change(function(){
		var value=$(this).val();
		if(value==0){
			$("#showJifen").html("固定积分：")	
		}else if(value==1){
			$("#showJifen").html("积分比率：")	
		}else {
			$("#showJifen").html("积分比率：")
			$("#chooseJifen").val(1)
		}
		
		$("#jifen").val("");
	})
})


function toAdd(){
	$("#doflag").val(1);
	resetForm();
	$("#yinying1,#window_change").show();
}

function initSelectlevel(){
	$("#levId").blur();
	var shopId = $("#shopName2").val();
	if(IsEmpty(shopId)){
		artError("请先选择店面");
		return false;
	}
	initSelecter({
		url : '../memberLevel/find/memberLevelList',
		data: {"shopId":shopId},
		heads : ["shopName", "Name"],
		titles : ["店名称", "级别名称"],
		zindex : 500,
		callBack : levelCallBack
	});
}

function levelCallBack(response, row){
	var id = response.getValue('ID', row);
	var name = response.getValue('Name', row);
	$("#levId").append("<option value='" + id + "' selected=\"selected\" >" + name + "</option>");
}
function toUpdate(id,shopId,obj){
	resetForm();
	$("#doflag").val(2);
	$("#updateId").val(id);
	for ( var i = 0; i < rst.result.data.length; i++) {
		var ids=getHtmlInt(getHeadIndexValue(i, rst, "RuleID"));
		if(id==ids){
			var option="<option value='"+getHtmlString(getHeadIndexValue(i, rst, "shopId"))+"'>"+getHtmlString(getHeadIndexValue(i, rst, "shopName"))+"</option>";
			$("#shopName2").html(option);
			option = "<option value='"+getHtmlString(getHeadIndexValue(i, rst, "levelID"))+"'>"+getHtmlString(getHeadIndexValue(i, rst, "levelName"))+"</option>";
			$("#levId").html(option);
			$("#RuleName").val(getHtmlString(getHeadIndexValue(i, rst, "RuleName")));
			$("#status").val(getHtmlString(getHeadIndexValue(i, rst, "Status")));
			$("#RuleType").val(getHtmlString(getHeadIndexValue(i, rst, "RuleType")));
			$("#schedName").val(getHtmlString(getHeadIndexValue(i, rst, "ScheduleTypeName")));
			_schedTypeId = _getStringOfJson(i, rst, "ScheduleTypeId");
			var ConsumeType=getHtmlString(getHeadIndexValue(i, rst, "ConsumeType"));
			if(ConsumeType=="0"){
				$("#chooseJifen").val("0")
				$("#showJifen").html("固定积分")
				$("#jifen").val(getHtmlString(getHeadIndexValue(i, rst, "FixedIntegral")));
			}else{
				$("#showJifen").html("积分比率")
				$("#chooseJifen").val("1")
				$("#jifen").val(getHtmlString(getHeadIndexValue(i, rst, "IntegralRate")));
			}
			
		}
	}
	
	$("#yinying1,#window_change").show();
}

function getParamToshopS(id,name){
	var htm="<option value='"+id+"'>"+name+"</option>"
	var shopflag=$("#shopflag").val();
	if(shopflag==1){
	$("#shopName").html(htm);
	$("#shopName").val(id);
	}else{
		$("#shopName2").html(htm);
		$("#shopName2").val(id);	
	}
	closeShopShow();
}

function closeShopShow(){
	$("#shopShow,#yinying").hide();
	$("#searchName").val("");
}

function InitShopList(shopPageIndex){
	var shopName=$("#searchName").val();
	$.ajax({
		type : "post",
		async:false,
		data :'name='+shopName,
		datatype : "json",
		url : "../shop/find/shopList/" + shopPageIndex + "/5",
		success : function(data) {
			var rst = JSON.parse(data);
			if(IsEmpty(rst)){
				art.dialog({fixed:true, lock: true,content:"查询出错",icon:'error',ok:true});	
				return false;
				}
				if(rst.state==0){
					$("#shopList tbody tr").remove();

					initShopTable(rst);
					$("table[id='shopList'] tbody tr").each(function(){
						$(this).children("td:last").css("text-align","center").append("<input type='button' class='common_btn' value='选择' onclick='bclicks($(this))'>");

					})	
					if(rst.result.len > 0){
						 var shu = rst.result.len;
						 shopCount = Math.ceil(shu/5);					
						 }else{
								shopCount==0;
						 }
					 $("#shopPager").pager({ pagenumber: ++shopPageIndex, pagecount: shopCount, buttonClickCallback: ShopPageClick });
					 addMargin($("#shopShow"));	
					 }else if(rst.state==1){
					art.dialog({fixed:true, lock: true,content:rst.msg,icon:'error',ok:true});	
				}else{
					checkStateShowErrorMsgAndDealResult(data);
				}
		}	
	});	
}

function InitTable() {
	var name=$("#searchName1").val().replace(/\s/gi,'');
	var status=$("#searchStatus").val().replace(/\s/gi,'');
	var shopId="";
	var param=null;
	if($("#shopIsChoosed").is(":checked")){
		shopId=$("#shopName").val();
		if(shopId!=""&&shopId!=null){
			param={'name':name,'shopId':shopId,'status':status};
		}else{
			param={'name':name,'status':status}
		}
	}else{
		param={'name':name,'status':status}
	}
	$.ajax({
		type : "get",
		async:false,
		data :param,
		datatype : "json",
		url : "../memberIntRule/find/memberIntegralRuleList/" + (pageIndex-1) + "/10",
		success : function(data) {
		 rst = JSON.parse(data);

					if(IsEmpty(rst)){
						art.dialog({fixed:true, lock: true,content:"查询出错",icon:'error',ok:true});	
						return false;
						}
						if(rst.state==0){
							$("#memberList tbody tr").remove();
							initDutyTable(rst);
							$("#memberList tbody tr").each(function(){
								var dj= $(this).find("td").eq(4)
								if(dj.html()==1){
									dj.html("积分比率")
								}else{
									dj.html("固定积分")
								}
							})
							
							if(rst.result.len > 0){
								 var shu = rst.result.len;
							 	 pagecount = Math.ceil(shu/10);
							 	 $("#NoResult").hide();
							}else{
								pagecount=0;
								$("#NoResult").show();
							}
							 	$("#sup").html("共"+rst.result.len+"条");	
							 $("#pager").pager({ pagenumber: pageIndex, pagecount: pagecount, buttonClickCallback: PageClick });
								
							 }else if(rst.state==1){
							art.dialog({fixed:true, lock: true,content:rst.msg,icon:'error',ok:true});	
						}else{
							checkStateShowErrorMsgAndDealResult(data);
						}				
		}	
	});
	
/* 	_pagination = new Pagination({
		url : '../memberIntRule/find/memberIntegralRuleList',
		data :param,
		success : printDataToPage,
		pageSize : 10
	}); */
}

ShopPageClick = function(pageclickednumber) {
    $("#shopPager").pager({ pagenumber: --pageclickednumber, pagecount: pagecount, buttonClickCallback: PageClick });
    $("#result").html("Clicked Page " + pageclickednumber);
    InitShopList(pageclickednumber);
}

PageClick = function(pageclickednumber) {
	pageIndex=pageclickednumber
	InitTable();
}

function search(){
	InitTable();
}
function search2(){
	InitShopList(0);
}
function initDutyTable(json){
	if(json!=null && json.state==0){
		var params = new Array("shopName", "RuleName","RuleTypeName","levelName","ConsumeType","IntegralRate","FixedIntegral","ScheduleTypeName","CreateFileTime","staName");// "PayCash", "PayPos", "PayDepstAmt", "PayCheck", "PayPersonCard", "PayIntAmtMny", "StaName", "PaidName"
		LoadDutytable(json,params,"memberList");
	}
}



function initShopTable(json){
	if(json!=null && json.state==0){
		var params = new Array("id", "name","mp","contact");
		Loadtable(json,params,"shopList");
	}
}


function LoadDutytable(json, params, tableId){
	var phArray = new Array();
	var count = 0;
	if (params != null) {
		$(params).each(function(pIndex, pData) {
			$.each(json.result.head, function(hIndex, hData) {
				if (pData == hData) {
					phArray[count++] = hIndex;
				}
			});
		});
	}
	if (json.result.data != null) {
		$.each(json.result.data, function(index, dt) {
			$("table[id='" + tableId + "']").append("<tr></tr>");
			$(phArray).each(function(phIndex, data) {
				$.each($(dt), function(dtIndex, d) {
					if (dtIndex == data) {
						$("table[id='" + tableId + "'] tr:last").append("<td>" +  (d === null||d=="null" ? " " : d) + "</td>");
					}
				});
			});
			var id=getHeadIndexValue(index, json, "RuleID");
			var htm="<td><input class=\"common_btn to_update\" type=\"button\" value=\"修改\" onclick='toUpdate("+id+","+getHeadIndexValue(index, json, "ShopID")+",$(this))' ><br><input class=\"del\" type=\"button\" value=\"删除\" onclick='popPoint("+id+")' ></td>"
			$("table[id='" + tableId + "'] tr:last").append(htm);
		});
	}
}

function Loadtable(json, params, tableId){
	var phArray = new Array();
	var count = 0;
	if (params != null) {
		$(params).each(function(pIndex, pData) {
			$.each(json.result.head, function(hIndex, hData) {
				if (pData == hData) {
					phArray[count++] = hIndex;
				}
			});
		});
	}
	if (json.result.data != null) {
		$.each(json.result.data, function(index, dt) {
			$("table[id='" + tableId + "']").append("<tr></tr>");
			$(phArray).each(function(phIndex, data) {
				$.each($(dt), function(dtIndex, d) {
					if (dtIndex == data) {
						$("table[id='" + tableId + "'] tr:last").append("<td>" +  (d === null||d=="null" ? " " : d) + "</td>");
					}
				});
			});
		});
	}
}

function showShopList(type){
	$("#shopflag").val(type)		
		$("#shopName").blur();
		$("#shopName2").blur();
	$("#shopShow,#yinying").show();
	InitShopList(0);
}

function  downRescouse(){
var code=$("#code").val().replace(/\s/gi,'');
	var shopId="";
	var param='code='+code;
	if($("#shopIsChoosed").is(":checked")){
		shopId=$("#shopName").val();
		if(shopId!=""&&shopId!=null){
			param+="&shopId="+shopId;
		}
	}
	param+="&downType=8"
	window.location.href="../csvdown/down/csv/down?"+param
}

function resetForm(){
	$("#levId").html("");
	$("#close_this").attr("disabled",false)
	$("#shopName2").html("");
	$("#RuleName").val("");
	$("#chooseJifen").val(0);
	$("#storedAmt").val("");
	$("#showJifen").html("固定积分");
	$("#jifen").val("");
	$("#schedName").val('')

	
}

function saveInfo(){
	var doflag=$("#doflag").val();
	if(doflag==1){
  		addInfo();
	}else if(doflag==2){
		updateInfo()
	}else{
			art.dialog({fixed:true, lock: true,content:"页面出错",icon:'error',ok:true});	
		window.location.reload();
	}
}

function addInfo(){
	var levId = $("#levId").val();	
	if (IsEmpty(levId)) {
		artError('请选择会员级别！');
		return false;
	}
 	if (IsEmpty(_schedTypeId)) {
		artError('请选择服务类型！');
		return false;
	}
 	
	var shopId=$("#shopName2").val();
	var RuleName=$("#RuleName").val().replace(/\s/gi,'');
	var RuleType=$("#RuleType").val().replace(/\s/gi,'');
	var jifen=$("#jifen").val();
	var status=$("#status").val().replace(/\s/gi,'');
	var chooseJifen=$("#chooseJifen").val();
	var FixedIntegral = 0;
	var IntegralRate = 0;

	var ruleType = $("#ruleType").val();
	if(checkNull(shopId,"店铺","shopName2")&&checkNull(RuleName,"规则名称","RuleName")){
	}else{
		return false
	}
	if(chooseJifen==0){
		if(!checkNumber(jifen, false, "固定积分")){
			return false
		}else{
			FixedIntegral = jifen;
			ConsumeType="固定积分"
		}
	}else{
		if(!_checkMoney(jifen, false, "积分比率")){
			return false
		}else{
			IntegralRate = jifen;
			ConsumeType="积分比率"
		}
	}
	

	
	
	var heads=new Array("shopId","RuleName","Status","ConsumeType","FixedIntegral","IntegralRate","RuleType","levelID")
	var datas=new Array(shopId,RuleName,status,chooseJifen,FixedIntegral,IntegralRate,RuleType,levId);

	var parser = new Object();
	var jsonParserStr = null;
	if (IsNotEmpty(datas)){
		addToParser(heads, datas, "add" , parser);
		jsonParserStr =  JSON.stringify(parser);
	}
	var heads2=new Array("schedId")
	var datas2=new Array();
	datas2.push(_schedTypeId)
	var json2=_getJsonParseInsertStr(heads2, datas2);
	var dt = _insertInfo("../memberIntRule/insert/insertInfo", {"data":jsonParserStr,"detl":json2})
			if(dt.state==0){
				pageIndex=1;
				InitTable();
				$("#yinying1,#window_change").hide();
				resetForm();
				 }
}
function deleteDuty(id){
	$.post("../memberIntRule/del/delInfo",{"id":id}, function(data) {
		var dt = JSON.parse(data);
		if(IsEmpty(dt)){
			art.dialog({fixed:true, lock: true,content:"数据删除错误",icon:'error',ok:true});	
			return false;
			}
			if(dt.state==0){
				InitTable();
				art.dialog({fixed:true, lock: true,content:"删除成功",icon:'succeed',ok:true});	
				 }else if(dt.state==1){
						art.dialog({fixed:true, lock: true,content:"数据删除错误",icon:'error',ok:true});	
			}else{
				checkStateShowErrorMsgAndDealResult(data);
			}
	});
}

function updateInfo(){
	
	var id=$("#updateId").val()
	if(IsEmpty(id)){
		art.dialog({fixed:true, lock: true,content:"修改出错",icon:'error',ok:true});	
		return false;
	}
	var levId = $("#levId").val();
	if (IsEmpty(levId)) {
		artError('请选择会员级别！');
		return false;
	}
	
 	if (IsEmpty(_schedTypeId)) {
		artError('请选择服务类型！');
		return false;
	} 
	
	var shopId=$("#shopName2").val();
	var RuleName=$("#RuleName").val().replace(/\s/gi,'');
	var RuleType=$("#RuleType").val().replace(/\s/gi,'');
	var jifen=$("#jifen").val();
	var status=$("#status").val().replace(/\s/gi,'');
	var chooseJifen=$("#chooseJifen").val();
	var FixedIntegral = 0;
	var IntegralRate = 0;
	var ruleType = $("#ruleType").val()

	if(checkNull(shopId,"店铺","shopName2")&&checkNull(RuleName,"规则名称","RuleName")){
	}else{
		return false;
	
	}
	if(chooseJifen==0){
		if(!checkNumber(jifen, false, "固定积分")){
			return false
		}else{
			FixedIntegral = jifen;
			ConsumeType="固定积分"
		}
	}else{
		if(!_checkMoney(jifen, false, "积分比率")){
			return false
		}else{
			IntegralRate = jifen;
			ConsumeType="积分比率"
		}
	}
	var heads=new Array("shopId","RuleName","status","ConsumeType","FixedIntegral","IntegralRate","RuleType","levelID","RuleID")
	var datas=new Array(shopId,RuleName,status,chooseJifen,FixedIntegral,IntegralRate,RuleType,levId,id);
	
	var heads2=new Array("schedId","ruleId")
	var datas2=new Array(_schedTypeId,id);

	var parser = new Object();
	var jsonParserStr = null;
	if (IsNotEmpty(datas)){
		addToParser(heads, datas, "edit" , parser);
		jsonParserStr =  JSON.stringify(parser);
	}
	
	var json2= _getJsonParseEditStr(heads2, datas2);
	$("#close_this").attr("disabled",true)
	var dt= _editInfoErrLog("../memberIntRule/edit/editInfo", {"data":jsonParserStr,"detl":json2});
	$("#close_this").attr("disabled",false)
			if(dt.state==0){
				InitTable();
				$("#yinying1,#window_change").hide();
				resetForm();
			}
}
function popPoint(id){
	art.dialog({fixed:true, lock: true,content:"确认删除该条数据？",icon:'question',ok: function(){ deleteDuty(id)},cancel:true});
	}
function showHideDiv(pageIndex){
	var url="../scheduleType/find/schdeuletTypeList/" + (pageIndex - 1) + "/5";
	var params=null;
	 _SchedTypejson=_findInfo(url, params);
	if(_SchedTypejson.state!=0){
		return false;
	}else{
		var params = new Array( "Name");
		_loadInfoOfChoose(_SchedTypejson,"actLargeList", "chooseSchedType", params,"ID")//加载选项页面
		_loadPage("goodsTypePager",pageIndex,5,_SchedTypejson,showHideDiv)//加载选项分页
		addMargin($("#window1"))
		$("#window1,#yinying").show()
	}
}
function chooseSchedType(id){
	_schedTypeId = id;
	for ( var i = 0; i < _SchedTypejson.result.data.length; i++) {
		var ids=_getIntOfJson(i, _SchedTypejson, "ID");
		if(ids==id){
			$("#schedName").val(_getStringOfJson(i, _SchedTypejson, "Name"));
		}
	}
	$("#window1,#yinying").hide()
}

</script>
</head>
<body>
<div id="yinying1"></div>
<div id="yinying"></div>
<div id="shopShow" class="hideDiv">
    <div style="float:right;margin-bottom:5px;width:20px;height:20px;line-height:20px;text-align:center;background-color:#eee;margin-bottom:5px;border:1px solid #ccc;" > <span style="cursor: pointer;font-size: 14px;" onclick="closeShopShow();" >X</span> </div>
    <div class="bottom_search"><div class="serWrapper"><input type="text" id="searchName" class="txt"><strong class="refresh" onclick="$('#searchName').val('')">清空</strong></div><input class="common_btn" type="button" style="width:auto;margin-left:5px;" value="查询" onclick="search2()"></div>
    <table   id="shopList" cellpadding="4" cellspacing="0" border="1" width="100%">
    <thead>
    <tr>
    <th>店面ID</th>
    <th>店面名称</th>
    <th>电话</th>
    <th>操作</th>
    </tr>
    </thead>
    </table>
    	<div id="shopPager"></div>
    </div>
    
 <div class="hideDiv hideOver" id="window1">
 <div class="closeTop"><span class="closeThe" onclick="closeParent($(this))"><img src="../images/close.png"></span></div>
	<table id="actLargeList" width="100%" cellspacing="1" cellpadding="0" border="0" class="member_list">
    	<caption>服务类型       </caption>
    	<thead>
    	<tr>
        	<th>类别</th>
        	<th>操作</th>
        </tr>
        </thead>
        <tbody>
        	
        </tbody>
    </table>
    <div id = "goodsTypePager" class="shopPager"></div>
</div>   
<!-- InstanceBeginEditable name="EditRegion3" -->
<form id="mainForm" >
  <div id="window_change" class="member_search hideDiv">
    <div class="closeTop"><span class="closeThe" onclick="closeParent($(this))"><img src="../images/close.png"></span></div>
    <input type="hidden" id="doflag">
    <input type="hidden" id="shopflag">
    <input type="hidden" id="defaultCflag">
    <input type="hidden" id="updateId">
    <input type="hidden" id="updateDtId">
    <p>
      <label><span> 店面：</span>
        <select id="shopName2" onclick="showShopList(2)" style=";height:20px;">
        	<option value="">请选择店铺</option>
        </select>
      </label>
       <label><span> 规则名称：</span>
               <input type="text" id="RuleName">
      </label>
    </p>
	<p>
    
       <label><span>计算类型：</span>
        <select id="chooseJifen" > <option value="1">积分比率</option><option value="0">固定积分</option></select>
      </label>
      <label><span>规则类别：</span>
        <select id="RuleType"><option value="1" >增加</option><option value="0">减少</option></select>
      </label>
    </p>
    <p>
      <label><span id="showJifen">固定积分：</span>
        <input type="text" id="jifen">
      </label>
    <label><span>服务类型：</span>
        <input type="text" id="schedName" onclick="showHideDiv(1)">
      </label>
    </p>
    <p>
         <label><span>会员级别：</span>
        <select id="levId" onclick="initSelectlevel()"></select>
     	</label>
		<label><span>状态：</span>
        <select id="status"><option value="1" >启用</option><option value="0">停用</option></select>
      	</label>
    </p>
    <div class="submit_btn">
      <input type="button" value="保存信息" class="common_btn" id="close_this" onclick="saveInfo()">
    </div>
  </div>
</form>
<div class="container" id="container">
  <div class="container_R fr">
	<div class="place">积分规则管理</div>

    <div class="member_search">
    <span> <input type="checkbox" id="shopIsChoosed" style="width:auto;height:auto;border:none;"> 店面：</span><select id="shopName" onclick="showShopList(1)" style=";height:20px;" disabled="disabled" >  </select>
     <span>积分规则名称：</span> <input type="text" id="searchName1" class="absoluteTxt"><span>状态：</span> <select id="searchStatus" style=";height:20px;" ><option value="">所有</option> <option value="1">正常</option><option value="0">禁用</option>  </select><input type="button" value="查询" class="btn" onclick="search()"></div>
    
    <table width="100%" class="member_list" cellpadding="0" cellspacing="1" border="0" id="memberList">
    	<caption><strong>积分规则列表        <span id="sup">共0条</span></strong><input type="button" value="添加规则" class="common_btn addList" onclick="toAdd()"></caption>
    	<thead>
    	<tr>
        	<th>店铺名称</th>
        	<th>规则名称</th>
        	<th>规则类别</th>
        	<th>级别名称</th>
        	<th>消费类型</th>
        	<th>积分比率</th>
        	<th>固定积分</th>
        	<th>服务类型</th>
        	<th>建档时间</th>
        	<th>规则状态</th>
        	<th>操作</th>
        </tr>
        </thead>
    </table>
    <div id="NoResult"  class="Nothing" style="text-align:center;display: none;">无符合条件的数据！</div>
    <div id="pager" style="float: right;"></div>
  </div>
  <div class="fc"></div>
</div>

<!-- InstanceEndEditable -->
<script type="text/javascript" src="../js/header.js"></script>
<script type="text/javascript" src="../js/left.js"></script>
<script type="text/javascript" src="../js/footer.js"></script>
<script type="text/javascript">
clearThis("absoluteTxt");
showTab(2,8);
</script>
</body>
<!-- InstanceEnd --></html>
